<?php
namespace app\index\controller;
use think\facade\Db;
use think\facade\View;
use think\facade\Cookie;
use app\bews\model\BewAdminConfig;

class Index{
	public function index(){
		# 获取配置
		$BewAdminConfig = new BewAdminConfig();
		$config = $BewAdminConfig->getAll();

		$ad = Db::table('oyk_ad')->where('status',1)->order('sort DESC,id DESC')->select();
		View::assign([
			'ad' => $ad,
			'config' => $config
		]);
		return View::fetch();
	}
	public function index_json(){
		$page = (int)input('post.page',1);
		$s = input('post.s');
		$where[] = ['status','=', 1];
		if(!empty($s)){
			$where[] = ['title','like', '%'.$s.'%'];
		}
		$class = Db::table('oyk_school_class')->where($where)->order('add_time DESC')->page($page,10)->select()->toArray();
		$tmp_class = [];
		$tmp_school = [];
		if(!empty($class)){
			foreach($class as $class_v){
				$tmp_class[] = $class_v['school_id'];
			}
			$school = Db::table('oyk_school')->where('school_id','in',$tmp_class)->column('*','school_id');
			if(!empty($school)){
				foreach($school as $school_v){
					$tmp_school[] = $school_v['uid'];
				}
				$user = Db::table('oyk_user')->where('uid','in',$tmp_school)->column('*','uid');
			}
			foreach($school as &$school_v){
				if($user[$school_v['uid']]){
					$school_v['user'] = $user[$school_v['uid']];
				}
			}
			foreach($class as &$class_v){
				if($school[$class_v['school_id']]){
					$class_v['school'] = $school[$class_v['school_id']];
				}
			}
		}
		$this->returnCode(0,$class);
	}
	public function details(){
		$id = (int)input('get.id',0);
		if(empty($id)){
			$this->errorTip('111');
		}
		$find = Db::table('oyk_school_class')->where('id',$id)->find();
		if(!empty($find)){
			$find['school'] = Db::table('oyk_school')->where('school_id',$find['school_id'])->find();
			$find['user'] = Db::table('oyk_user')->where('uid',$find['school']['uid'])->find();
			$find['intr_s'] = explode(';',$find['intr']);
		}
		View::assign([
			'find' => $find,
			'id' => $id
		]);
		return View::fetch();
	}
	public function login(){
		return View::fetch();
	}
	public function login_json(){
		$phone = input('post.phone');
		if(empty($phone)){
			$this->returnCode(1,'请输入手机号');
		}
		$pwd = input('post.pwd');
		if(empty($pwd)){
			$this->returnCode(1,'请输入密码');
		}
		$find = Db::table('oyk_user')->where('phone',$phone)->find();
		if(empty($find)){
			$this->returnCode(1,'未找到账号');
		}
		if($find['pwd'] != md5($pwd)){
			$this->returnCode(1,'密码不正确');
		}
		$remember = input('post.remember');
		if(!empty($remember)){
			Cookie::set('uid',$find['uid'],60*60*24*7);
		}else{
			Cookie::set('uid',$find['uid']);
		}
		$this->returnCode(0);
	}
	public function reg(){
		return View::fetch();
	}
	public function reg_json(){
		$phone = input('post.phone');
		if(empty($phone)){
			$this->returnCode(1,'请输入手机号');
		}
		$pwd = input('post.pwd');
		if(empty($pwd)){
			$this->returnCode(1,'请输入密码');
		}
		$find = Db::table('oyk_user')->where('phone',$phone)->find();
		if(!empty($find)){
			$this->returnCode(1,'手机号已存在');
		}
		$id = Db::table('oyk_user')->insertGetId([
			'phone' => $phone,
			'nickname' => 'bmxt'.date('YmdHis',time()).mt_rand(100,999),
			'pwd' => md5($pwd),
			'add_time' => time(),
			'last_time' => time()
		]);
		if(empty($id)){
			$this->returnCode(1,'注册失败，请重试');
		}
		Cookie::set('uid',$id);
		$this->returnCode(0);
	}
	# 图片上传
	public function upload_img(){
		$file = request()->file();
		$files = request()->file('file');
		if($file==null){
			exit(json_encode(array('code'=>1,'msg'=>'没有文件上传')));
		}
		try {
			validate(['image'=>'filesize:10240|fileExt:jpg,png,gif,jpeg'])->check($file);
			$info = \think\facade\Filesystem::disk('public')->putFile('bews',$files);
		} catch (\think\exception\ValidateException $e) {
			exit(json_encode(array('code'=>1,'msg'=>$e->getMessage())));
		}
		$info = str_replace("\\","/",$info);
		$img = '/storage/'.$info;
		exit(json_encode(array('code'=>0,'data'=>$img)));
	}
	/**
	 * 返回json对象
	 */
	protected function returnCode($code,$data=[],$count=10){
		header('Content-type:application/json');
		if($code == 0){
			$arr = array(
				'code'=>$code,
				'msg'=>'成功',
				'count'=> $count,
				'data' => $data
			);
		}else if($code >= 1 && $code <= 100){
			$arr = array(
				'code'	=>	$code,
				'msg'	=>	$data
			);
		}else{
			$appapi = new AppApi();
			$arr = array(
				'code'=>$code,
				'msg'=>$appapi::errorTip($code)
			);
		}
		echo json_encode($arr);
		if($code != 0){
			exit;
		}
	}
}